<template>
  <el-drawer v-model="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false"
             :close-on-press-escape="false" size="900px">
    <el-form :model="dataForm" :rules="rules" ref="dataFormRef"
             :label-width="$i18n.locale === 'en-US' ? '120px' : '100px'">
      <el-form-item label="考试名称：" prop="name">
        <el-input v-model="dataForm.name" placeholder="请输入考试名称"></el-input>
      </el-form-item>
      <el-form-item label="考试周期：" prop="value2">
        <el-date-picker
          v-model="dataForm.value2"
          type="datetimerange"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
          format="YYYY-MM-DD HH:mm:ss"
          value-format="YYYY-MM-DD HH:mm:ss"
          date-format="YYYY/MM/DD ddd"
          time-format="A hh:mm:ss"
          @change="timeChange"
        />
      </el-form-item>
      <el-form-item label="选择题库：" prop="bankId">
        <el-select v-model="dataForm.bankId" placeholder="选择题库" @change="selectChange">
          <el-option
            v-for="item in list"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="题库分数：">
        {{ soce }}
      </el-form-item>
      <el-form-item label="分数线：" prop="scoreLine">
        <el-input-number v-model="dataForm.scoreLine" :min="1" :max="soce"/>
      </el-form-item>
      <el-form-item label="加分分数：" prop="scoreParty">
        <el-input-number v-model="dataForm.scoreParty" :min="1" :max="10"/>
      </el-form-item>
      <el-form-item label="备注：" prop="remark">
        <el-input v-model="dataForm.remark" placeholder="请输入备注"></el-input>
      </el-form-item>
    </el-form>
    <template v-slot:footer>
      <el-button @click="visible = false">{{ $t("cancel") }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle">提交</el-button>
    </template>
  </el-drawer>
</template>

<script lang="ts" setup>
import {reactive, ref} from "vue";
import baseService from "@/service/baseService";
import {useI18n} from "vue-i18n";
import {ElMessage} from "element-plus";
import {useRoute} from "vue-router";

const {query} = useRoute();
const {t} = useI18n();
const emit = defineEmits(["refreshDataList"]);

const visible = ref(false);
const dataFormRef = ref();
const soce = ref(0);

const value = ref();

const dataForm = reactive({
  id: "",
  name: "",
  remark: "",
  value2: [],
  bankId: "",
  startTime: "",
  endTime: "",
  scoreLine: 0,
  scoreParty: 1,
  examReleaseQuestionList: [] //题目列表
}) as any;

const rules = ref({
  name: [{required: true, message: t("validate.required"), trigger: "blur"}],
  value2: [{required: true, message: t("validate.required"), trigger: "change"}],
  bankId: [{required: true, message: t("validate.required"), trigger: "change"}]
});

const selectChange = (e: any) => {
  soce.value = list.value.filter((item: any) => item.id === e)[0].score;
  dataForm.scoreLine = Math.floor(soce.value * 0.8);
}

const init = (id: number) => {
  visible.value = true;
  dataForm.id = "";
  soce.value = 0;
  // 重置表单数据
  if (dataFormRef.value) {
    dataFormRef.value.resetFields();
  }
  getDataList();
  if (id) {
    getInfo(id);
  }
};

const list = ref();

const getDataList = () => {
  baseService.get(`/clc/clcquestionbank/page`, {limit: 300, page: 1}).then((res) => {
    list.value = res.data.list;
  });
}

const timeChange = (e: any) => {
  if (e) {
    dataForm.startTime = e[0];
    dataForm.endTime = e[1];
  } else {
    dataForm.startTime = "";
    dataForm.endTime = "";
  }
}

// 获取信息
const getInfo = (id: number) => {
  baseService.get(`/clc/clcexamrelease/${id}`).then((res) => {
    soce.value = res.data.scoreTotal;
    Object.assign(dataForm, res.data);
    dataForm.value2 = [dataForm.startTime, dataForm.endTime];
  });
};

// 表单提交
const dataFormSubmitHandle = () => {
  dataFormRef.value.validate((valid: boolean) => {
    if (!valid) {
      return false;
    }
    delete dataForm.value2;
    (!dataForm.id ? baseService.post : baseService.put)("/clc/clcexamrelease", dataForm).then((res) => {
      ElMessage.success({
        message: t("prompt.success"),
        duration: 500,
        onClose: () => {
          visible.value = false;
          emit("refreshDataList");
        }
      });
    });
  });
};

defineExpose({
  init
});
</script>
